package com.yxw.yxnet_cd_center.customer_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import com.yxw.yxnet_cd_center.common.utils.BaseDateTime;
import com.yxw.yxnet_cd_center.customer_service.dto.VoiceLibAddDTO;
import com.yxw.yxnet_cd_center.customer_service.dto.VoiceLibQueryDTO;
import com.yxw.yxnet_cd_center.customer_service.entity.YxCsVoiceLibEntity;
import com.yxw.yxnet_cd_center.customer_service.mapper.VoiceLibMapper;
import com.yxw.yxnet_cd_center.customer_service.service.VoiceLibService;
import com.yxw.yxnet_cd_center.customer_service.utils.PageUtils;
import com.yxw.yxnet_cd_center.customer_service.utils.UserUtil;
import com.yxw.yxnet_cd_center.customer_service.vo.YxCsGroupVo;
import com.yxw.yxnet_cd_center.customer_service.vo.YxCsVoiceLibVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 语音库
 * @author liuxin
 * @date 2023.09.17
 */
@Service
public class VoiceLibServiceImpl  extends ServiceImpl<VoiceLibMapper, YxCsVoiceLibEntity> implements VoiceLibService {

    @Autowired
    private VoiceLibMapper voiceLibMapper;

    @Autowired
    private UserUtil userUtil;

    /**
     * 语音库列表查询
     * @param dto
     * @return
     */
    @Override
    public PageVO<YxCsVoiceLibVo> getVoiceLibList(VoiceLibQueryDTO dto) {
        Page<YxCsVoiceLibEntity> page = new Page<>(dto.getCurrPage(), dto.getPageSize());
        LambdaQueryWrapper<YxCsVoiceLibEntity> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dto.getVoiceLibName())){
            queryWrapper.like(true,YxCsVoiceLibEntity::getVoiceLibName,dto.getVoiceLibName().trim());
        }
        queryWrapper.eq(null!=dto.getStatus(),YxCsVoiceLibEntity::getStatus,dto.getStatus()).
                ge(StringUtils.isNotBlank(dto.getCreateTimeStart()), YxCsVoiceLibEntity::getCreateTime, BaseDateTime.getDate(dto.getCreateTimeStart())).
                le(StringUtils.isNotBlank(dto.getCreateTimeEnd()), YxCsVoiceLibEntity::getCreateTime, BaseDateTime.getDate(dto.getCreateTimeEnd())).
                orderByDesc(true,YxCsVoiceLibEntity::getCreateTime);
        page=baseMapper.selectPage(page,queryWrapper);
        List<YxCsVoiceLibEntity> entityList = page.getRecords();
        List<YxCsVoiceLibVo> listVoList =new ArrayList<>();
        if(null!=entityList && !entityList.isEmpty()){
            listVoList = BeanUtil.copyToList(entityList, YxCsVoiceLibVo.class);
        }
        return PageUtils.get(page,listVoList);
    }

    /**
     * 新增语音库
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public YxCsVoiceLibVo addVoiceLib(VoiceLibAddDTO dto) {
        YxCsVoiceLibEntity entity = new YxCsVoiceLibEntity();
        BeanUtil.copyProperties(dto,entity);
        entity.setCreateId(userUtil.getLoginUserId());
        entity.setCreateName(userUtil.getLoginName());
        entity.setCreateTime(new Date());
        baseMapper.insert(entity);
        YxCsVoiceLibVo vo = new YxCsVoiceLibVo();
        BeanUtil.copyProperties(entity,vo);
        return vo;
    }

    /**
     * 删除语音库
     * @param voiceLibId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVoiceLibById(Long voiceLibId) {
        if(null==voiceLibId){
            throw new BaseException("删除主键不能为空");
        }
        //删除数据
        baseMapper.deleteById(voiceLibId);
    }

    /**
     * 语音库状态修改
     * @param voiceLibId
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatusById(Long voiceLibId) {
        baseMapper.updateStatusById(voiceLibId);
    }
}
